package org.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.example.config.UserContextConfig;
import org.example.error.BusinessException;
import org.example.error.EmBusinessError;
import org.example.service.model.UserModel;

/**
 * @author xietl
 * @date 2025/10/8  15:46
 * @description 用户认证切面  * 用于校验用户是否登录
 */
@Aspect   // 声明这是一个切面类，用于拦截带有@UserLoginToken注解的方法
public class UserAuthAspect {
    /**
     *环绕通知，校验用户是否登录
     * 拦截带有@UserLoginToken注解的方法
     */
    @Around("@annotation(org.example.aop.UserLoginToken)")   // 拦截带有@UserLoginToken注解的方法
    public Object checkUserLogin(ProceedingJoinPoint joinPoint) throws Throwable{
        UserModel userModel = UserContextConfig.getUserModel();
        if(userModel==null){
            throw new BusinessException(EmBusinessError.USER_NOT_LOGIN,EmBusinessError.USER_NOT_LOGIN.getErrMsg());
        }
        //继续执行原方法
        return  joinPoint.proceed();
    }
}
